package com.logic.landseaserver.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.domain.Issue;
import com.logic.landseaserver.persistence.read.IssueReadMapper;
import com.logic.landseaserver.persistence.write.IssueWriteMapper;
import com.logic.landseaserver.service.IIssueService;
import com.logic.landseaserver.ws.dto.IssueDTO;

@Service
public class IssueServiceImpl implements IIssueService {

	@Autowired
	private IssueReadMapper issueReadMapper;

	@Autowired
	private IssueWriteMapper issueWriteMapper;

	@Override
	public void delete(String arg0) throws BusinessException {
	    String[] idArr = arg0.split(",");
	    for (String id : idArr) {
	      Integer issueId = Integer.valueOf(id);
	      Issue issue = (Issue) this.get(issueId);
	      Boolean pass = this.validateForDelete(issue);
	      if (pass) {
	    	  issue.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
	    	  issueWriteMapper.deleteByPrimaryKey(issue);
	      }
	    }

	}

	@Override
	public Object get(Integer arg0) throws BusinessException {
		return issueReadMapper.selectByPrimaryKey(arg0);
	}

	@Override
	public int insert(Object arg0) throws BusinessException {
		Integer id = -1;
		Boolean pass = this.validateForInsert(arg0);
		if (pass) {
			Issue issue = (Issue) arg0;
			issue.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			issueWriteMapper.insertSelective(issue);
			id = issue.getId();
		}
		return id;
	}

	@Override
	public List<?> query(QueryUtil arg0) {
		return issueReadMapper.getIssueList(arg0);
	}

	@Override
	public void update(Object arg0) throws BusinessException {
		Boolean pass = this.validateForUpdate(arg0);
		if (pass) {
			Issue issue = (Issue) arg0;
			issue.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			issue.setCreatedBy(null);
			issue.setCreatedDtm(null);
			issueWriteMapper.updateByPrimaryKeySelective(issue);
		}

	}

	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		Boolean pass = true;
		if (arg0 == null) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_Insert_Or_Update_Empty_Object);
		}
		if (!(arg0 instanceof Issue)) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_Insert_Or_Update_Type);
		}
		Issue issue = (Issue) arg0;
		if (issue.getTemplate() != null && issue.getTemplate()) {
			Issue nameIssue = issueReadMapper.selectByName(issue.getName());
			if (nameIssue != null && !nameIssue.getId().equals(issue.getId())) {
				throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_Insert_Or_Update_Name_Repeat);
			}
		}
		return pass;
	}

	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		return true;
	}

	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		Issue issue = (Issue)arg0;
		if (issue.getId() != null) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_ID);
		}
		if (StringUtils.isBlank(issue.getName())) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_Issue_Name);
		}
		if (StringUtils.isBlank(issue.getSerialNum())) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_Issue_Number);
		}
		if (issue.getMinImageNumber() == null) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_Issue_Min_Image_Number);
		}
		if (StringUtils.isBlank(issue.getType())) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_Issue_Type);
		}
		if (StringUtils.isBlank(issue.getDescription())) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_CreateIssue_Empty_Issue_Description);
		}
		return pass;
	}

	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		Issue issue = (Issue)arg0;
		if (issue.getId() == null) {
			throw new LandseaBusinessException(LandseaErrorMessageEnum.Issue_UpdateIssue_Empty_Issue_ID);
		}
		return pass;
	}


	@Override
	public IssueDTO getDTO(Integer id) throws BusinessException {
		return issueReadMapper.selectDTOByPrimaryKey(id);
	}

	@Override
	public List<IssueDTO> queryDTO(QueryUtil queryUtil) {
		return issueReadMapper.getIssueDTOList(queryUtil);
	}

	@Override
	public Issue copyFromTemplate(Integer issueId) throws BusinessException {
		Issue template = (Issue) this.get(issueId);
		Issue issue = this.buildIssue(template);
		this.insert(issue);
		return issue;
	}

	//prepare for copy issue from template
	private Issue buildIssue(Issue template) {
		Issue issue = new Issue();
		issue.setType(template.getType());
		issue.setTemplate(false);
		issue.setSerialNum(template.getSerialNum());
		issue.setName(template.getName());
		issue.setMinImageNumber(template.getMinImageNumber());
		issue.setDescription(template.getDescription());
		return issue;
	}

	@Override
	public void add2Template(Issue issue) throws BusinessException {
		//add to template
		issue.setId(null);
		this.insert(issue);
	}

	@Override
	public List<Issue> queryByCheckId(QueryUtil queryUtil) {
		return issueReadMapper.queryByCheckId(queryUtil);
	}

	@Override
	public Integer countByCheckId(Integer checkId) {
		return issueReadMapper.countByCheckId(checkId);
	}

	@Override
	public List<Issue> getByCheckId(Integer checkId) {
		return issueReadMapper.getByCheckId(checkId);
	}

}
